﻿
/*============== simpleIRC - http://matthias.van-eeghem.com/ ==============*
 *                                                                         *
 *  File: QueryWindow.cs                                                   *
 *  Project: simpleIRC                                                     *
 *  Author(s): Matthias Van Eeghem <matthias@van-eeghem.com>               *
 *                                                                         *
 * ========================================================================*
 *                                                                         *
 *  This program is free software: you can redistribute it and/or modify   *
 *  it under the terms of the GNU General Public License as published by   *
 *  the Free Software Foundation, either version 3 of the License, or      *
 *  (at your option) any later version.                                    *
 *                                                                         *
 *  This program is distributed in the hope that it will be useful,        *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
 *  GNU General Public License for more details.                           *
 *                                                                         *
 *  You should have received a copy of the GNU General Public License      *
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.  *
 *                                                                         *
===========================================================================*/

using System;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Threading;
using simpleIRC;

namespace simpleIRC.UI
{
    /// <summary>
    /// This class contains all UI stuff related to servers
    /// </summary>

    public class QueryWindow
    {
        /// <summary>
        /// This variable holds a reference to the MainWindow class
        /// </summary>
        MainWindow mainWindow = null;

        /// <summary>
        /// The ServerIndex the query is on
        /// </summary>
        private int ServerIndex;

        /// <summary>
        /// The name of the QueryWindow
        /// </summary>
        private string Name;

        /// <summary>
        /// The Query object this QueryWindow links to
        /// </summary>
        private Query Query;

        /// <summary>
        /// This TreeViewItem holds the TreeViewItem for this QueryWindow object
        /// </summary>
        public TreeViewItem QueryTreeViewItem { get; private set; }

        /// <summary>
        /// This static variable holds a list of all the QueryWindows created
        /// </summary>
        public static List<QueryWindow> List = new List<QueryWindow>();

        /// <summary>
        /// The constructor of the QueryWindow class
        /// </summary>
        /// <param name="name">The channel name</param>
        /// <param name="serverIndex">The serverindex the querywindow is on</param>
        /// <param name="queryIndex">The index of the Query</param>
        /// <param name="parentWindow">The reference to the MainWindow object</param>

        public QueryWindow(string name, int serverIndex, int queryIndex, MainWindow parentWindow)
        {
            Name = name;
            mainWindow = parentWindow;
            Query = Core.QueryList[queryIndex];
            ServerIndex = serverIndex;

            // Add this object to the QueryWindowList
            List.Add(this);
        }

        /// <summary>
        /// This method creates the actual TreeViewItem to the ChannelsTreeView
        /// </summary>

        public void Create()
        {
            QueryTreeViewItem = new TreeViewItem();
            QueryTreeViewItem.Header = Name;

            TreeViewItem QueryTreeViewItemCopy = QueryTreeViewItem;
            mainWindow.UserInterface.AddRightClickEvent(ref QueryTreeViewItemCopy, "Close", mainWindow.UserInterface.ContextMenu_CloseQueryWindow);

            // Select the TreeViewItem
            QueryTreeViewItem.IsSelected = true;
            ServerWindow.List[ServerIndex].QueryListTreeViewItem.Items.Add(QueryTreeViewItem);

            mainWindow.CurrentChannelWindow = -1;
            mainWindow.CurrentQueryWindow = Query.Index;
            mainWindow.CurrentServerWindow = ServerIndex;

            int Count = 0;

            for (int i = 0; i < Core.QueryList.Count; i++)
            {
                if (Core.QueryList[i] != null && Core.QueryList[i].Server.Index == ServerIndex)
                    Count++;
            }

            // Update the amount of queries in the header.
            ServerWindow.List[ServerIndex].QueryListTreeViewItem.Header = "Query: " + Count;
        }

        /// <summary>
        /// This function appends a string to the Query's text
        /// </summary>
        /// <param name="outputLine">The outputline to write</param>
        /// <param name="appendTimestamp">If this bool is set to true, the method prepends a timestamp before the string</param>

        public void SendText(string outputLine, bool appendTimestamp = true)
        {
            if (appendTimestamp) mainWindow.UserInterface.FormatTimestamp(ref outputLine);

            Query.Text += outputLine;

            // We're currently looking at the window it's supposed to go to, just write to it!
            if (mainWindow.CurrentQueryWindow == Query.Index)
            {
                mainWindow.ChatBox.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate() { mainWindow.Colors.HandleColors(outputLine); }));
            }

            // We're looking at another server window than the one we're supposed to write to.
            else
            {
                mainWindow.ChatBox.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate() { QueryWindow.List[Query.Index].QueryTreeViewItem.Foreground = Brushes.Red; }));
                Sounds.Play(Sounds.PONG_SOUND);
            }
        }

        /// <summary>
        /// This method removes the QueryWindow from the TreeView
        /// </summary>

        public void Remove()
        {
            // Remove the query treeviewitem
            ServerWindow.List[ServerIndex].QueryListTreeViewItem.Items.Remove(QueryTreeViewItem);

            int Count = 0;

            for (int i = 0; i < Core.QueryList.Count; i++)
            {
                if (Core.QueryList[i] != null && Core.QueryList[i].Server.Index == ServerIndex)
                    Count++;
            }

            // Update the amount of queries in the header.
            ServerWindow.List[ServerIndex].QueryListTreeViewItem.Header = "Query: " + (Count - 1);
        }
    }
}
